package admin;

import enums.DeletedStatus;
import models.base.BaseModel;
import org.apache.commons.lang.StringUtils;
import param.admin.AdminBusinessRequest;

import javax.persistence.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static helper.BeanHelper.safeSetValue;

/**
 * Created by liming on 16/6/29.
 */
@Entity
@Table(name = "admin_business")
public class AdminBusiness extends BaseModel {


    /**
     * 业务编号
     */
    @Column(name = "code")
    public String code;

    /**
     * 业务名称
     */
    @Column(name = "name")
    public String name;


    /**
     * Controller名称
     */
    @Column(name = "controller_name")
    public String controllerName;


    /**
     * action 名称
     */
    @Column(name = "action_name")
    public String actionName;


    /**
     * 父节点ID
     */
    @Column(name = "parent_id")
    public String parentId;

    /**
     * 是否为叶子节点
     */
    @Column(name = "is_leaf")
    public Boolean isLeaf;


    /**
     * 节点全路径
     * 格式：/parentNodePath/id/
     * 如果是根节点 /id/
     */
    @Column(name = "node_id_path")
    public String nodeIdPath;

    /**
     * 排序号
     */
    @Column(name = "show_order")
    public Integer showOrder = 0;

    @Column(name = "remark")
    public String remark;

    /**
     * 逻辑删除,0:未删除，1:已删除
     */
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    /**
     * 创建时间.
     */
    @Column(name = "created_at")
    public Date createdAt;

    /**
     * 创建人.
     */
    @ManyToOne
    @JoinColumn(name = "create_user_id")
    public AdminUser createUser;


    /**
     * 更新时间.
     */
    @Column(name = "modify_at")
    public Date modifyAt;

    /**
     * 修改人.
     */
    @ManyToOne
    @JoinColumn(name = "modify_user_id")
    public AdminUser modifyUser;


    /**
     * 孩子节点
     */
    @Transient
    public List<AdminBusiness> childrenNodes ;



    /**
     * 创建业务 、功能
     * @param businessRequest
     * @return
     */
    public static AdminBusiness create(AdminBusinessRequest businessRequest, AdminUser loginUser){
        AdminBusiness business = new AdminBusiness();
        safeSetValue(businessRequest.code, value -> business.code = value );
        safeSetValue(businessRequest.name, value -> business.name = value );
        safeSetValue(businessRequest.controllerName, value -> business.controllerName = value );
        safeSetValue(businessRequest.actionName, value -> business.actionName = value );
        if(StringUtils.isNotBlank(businessRequest.parentId)){
            safeSetValue(true, value -> business.isLeaf = value );
            safeSetValue(businessRequest.parentId, value -> business.parentId = value );
        }else{
            safeSetValue(false, value -> business.isLeaf = value );
            safeSetValue("0", value -> business.parentId = value );
        }
        safeSetValue(businessRequest.nodeIdPath, value -> business.nodeIdPath = value );
        safeSetValue(businessRequest.showOrder, value -> business.showOrder = value );
        safeSetValue(businessRequest.remark, value -> business.remark = value );

        business.isLeaf = true ; // 新创建的节点都是叶子节点
        business.deleted = DeletedStatus.UN_DELETED ; //新创建的节点deleted 状态都是未删除
        business.createdAt = new Date() ;
        business.createUser = loginUser ;
        business.save();
//        //寻找该节点的父亲节点，如果父亲节点是叶子节点，则改为非叶子节点
//        AdminBusiness parentNode = AdminBusiness.findByBusinessId(business.parentId);
//        if(parentNode != null && parentNode.isLeaf) {
//            parentNode.isLeaf = false;
//            parentNode.save();
//        }
        return business ;
    }

    /**
     * 修改
     * @param id
     * @param businessRequest
     * @return
     */
    public  static AdminBusiness modfiy(String id , AdminBusinessRequest businessRequest , AdminUser loginUser){
        AdminBusiness business = AdminBusiness.findById(id);
        safeSetValue(businessRequest.name, value -> business.name = value );
        safeSetValue(businessRequest.controllerName, value -> business.controllerName = value );
        safeSetValue(businessRequest.actionName, value -> business.actionName = value );
        safeSetValue(businessRequest.isLeaf, value -> business.isLeaf = value );
        safeSetValue(businessRequest.parentId, value -> business.parentId = value );
        safeSetValue(businessRequest.nodeIdPath, value -> business.nodeIdPath = value );
        safeSetValue(businessRequest.showOrder, value -> business.showOrder = value );
        safeSetValue(businessRequest.remark, value -> business.remark = value );

        if(StringUtils.isNotBlank(businessRequest.deleted)) {
            business.deleted = "UN_DELETED".equals(businessRequest.deleted) ? DeletedStatus.UN_DELETED : DeletedStatus.DELETED;
        }
        business.modifyUser = loginUser ;
        business.modifyAt = new Date() ;
        business.save() ;

        return business ;

    }

    public static AdminBusiness findByBusinessId(String id){
        return AdminBusiness.find("deleted = ? and id = ? " , DeletedStatus.UN_DELETED , id).first();
    }

    /**
     * 根据父节点ID 查询
     * @param parentId
     * @return
     */
    public static List<AdminBusiness>  loadAllBusinessByParentId(String parentId){
        return AdminBusiness.find("deleted = ? and parentId = ?" , DeletedStatus.UN_DELETED,parentId).fetch();
    }


    /**
     * 根据名称查询根节点
     * @param name
     * @return
     */
    public static List<AdminBusiness>  loadAllBusiness(String name){
        return AdminBusiness.find("deleted = ? and name like ? and parentId = '0' " , DeletedStatus.UN_DELETED,name).fetch();
    }

    //获取所有业务
    public static List<AdminBusiness>  loadAllBusiness(){
        return AdminBusiness.find("deleted = ? " , DeletedStatus.UN_DELETED).fetch();
    }

    //检查是否有兄弟节点
    public static long checkBrotherNode(String parentId){
        return  AdminBusiness.count(" deleted = ? and parentId = ?" , DeletedStatus.UN_DELETED , parentId)  ;
    }
}
